package com.abcdefg.rabbitmqextensionsdemo.listener;

import com.abcdefg.rabbitmqextensionsdemo.constants.Constant;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

//@Component
public class Listener {
    @RabbitListener(queues = Constant.DIRECT_QUEUE_NAME)
    public void listen(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {

            System.out.printf("接收到的消息：%s,deliveryTag：%d\n", new String(message.getBody())
                    , message.getMessageProperties().getDeliveryTag());
            System.out.println("处理业务逻辑！！！");
            int sum=10/0;
            System.out.println("处理完成！！！");
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            //进行异常确认
            channel.basicNack(deliveryTag,false,false);
        }
    }
}
